//
//  HRZHomeRecommendPageViewController.m
//  EMarket
//
//  Created by 石头 on 2018/5/15.
//  Copyright © 2018年 好日子. All rights reserved.
//

#import "HRZHomeRecommendPageViewController.h"

// Controllers
#import "HRZGoodsDetailViewController.h"
#import "HRZStoreViewController.h"
// Models

// Views
#import "HRZRecommendSupplierCell.h"

// Vendors
#import <MJExtension.h>
#import "SDCycleScrollView.h"

// Categories
#import "UIView+SPExtension.h"
// Others

#define RecommendTypeCount  6

static NSString * const HRZRecommendSupplierCellID = @"HRZRecommendSupplierCell";

@interface HRZHomeRecommendPageViewController ()<SDCycleScrollViewDelegate, UITableViewDelegate, UITableViewDataSource, HRZRecommendSupplierCellDelegate>
{
    NSArray *_goodsTypeArray;
}


@property (nonatomic, strong) UIView *topSearchView;
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) UIButton *messageButton;

@property (nonatomic, strong) UITableView *tableView;

/** 广告页面 */
@property (nonatomic, strong) SDCycleScrollView *bannerAd;


@end

@implementation HRZHomeRecommendPageViewController
@synthesize tableView = _tableView;
#pragma mark - LazyLoad
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        CGRect fram = CGRectMake(0, 0, kScreenWidth, kScreenHeight - HRZTopNavH - HRZBottomTabH - 40);
        _tableView.frame = fram;
        _tableView.tableFooterView = [UIView new];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        // 注册cell
        [_tableView registerNib:[UINib nibWithNibName:@"HRZRecommendSupplierCell" bundle:nil] forCellReuseIdentifier:HRZRecommendSupplierCellID];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (SDCycleScrollView *)bannerAd{
    NSArray *imagesURLStrings = @[@"http://eimg.smzdm.com/201706/21/5949c794a058193.png",
                                  @"http://eimg.smzdm.com/201706/20/594872b3ce4897616.png",
                                  @"http://eimg.smzdm.com/201706/21/5949c759c9ec75211.png",
                                  @"http://eimg.smzdm.com/201706/20/59490304c13471312.jpg",
                                  @"http://eimg.smzdm.com/201706/20/5948c8333d5242625.png"];
    
    if (!_bannerAd) {
        _bannerAd = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, 180) delegate:self placeholderImage:[UIImage imageNamed:@"default"]];
        _bannerAd.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        _bannerAd.currentPageDotColor = [UIColor whiteColor];
        _bannerAd.imageURLStringsGroup = imagesURLStrings;
    }
    
    return _bannerAd;
}

#pragma mark - LifeCyle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    
    [self setNaviBar];
    
    [self setupData];
    
    [self setupUI];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Public

#pragma mark - Private
/** 设置UI */
- (void)setupUI {
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.estimatedRowHeight = 80.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

/** 获取数据 */
- (void)setupData {
    
}


- (void)setNaviBar {
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -15;
    
    UIButton *button = [[UIButton alloc] init];
    [button setImage:[UIImage imageNamed:@"mshop_message_gray_icon"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"mshop_message_gray_icon"] forState:UIControlStateHighlighted];
    button.frame = CGRectMake(0, 0, 44, 44);
//    [button addTarget:self action:@selector(messButtonBarItemClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    self.navigationItem.rightBarButtonItems = @[negativeSpacer, backButton];
    
    _topSearchView = [[UIView alloc] init];
    _topSearchView.backgroundColor = HRZBGColor;
    _topSearchView.layer.cornerRadius = 5;
    [_topSearchView.layer masksToBounds];
    _topSearchView.frame = CGRectMake(10, 26, kScreenWidth - 54, 32);
    [self.view addSubview:_topSearchView];
    
    _searchButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_searchButton setImage:[UIImage imageNamed:@"icon_search"] forState:UIControlStateNormal];
    [_searchButton setTitle:@"搜索商品" forState:0];
    [_searchButton setTitleColor:[UIColor lightGrayColor] forState:0];
    _searchButton.titleLabel.font = PFR12Font;
    [_searchButton adjustsImageWhenHighlighted];
    _searchButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _searchButton.titleEdgeInsets = UIEdgeInsetsMake(0, 2* HRZMargin, 0, 0);
    _searchButton.imageEdgeInsets = UIEdgeInsetsMake(0, HRZMargin, 0, 0);
//    [_searchButton addTarget:self action:@selector(onSearchButtonClick) forControlEvents:UIControlEventTouchUpInside];
    _searchButton.frame = CGRectMake(0, 0, _topSearchView.sp_width - 2 * HRZMargin, _topSearchView.sp_height);
    [_topSearchView addSubview:_searchButton];
    
    _messageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_messageButton setTitle:@"🆕" forState:0];
    [_messageButton setTitleColor:[UIColor lightGrayColor] forState:0];
    _messageButton.titleLabel.font = PFR12Font;
    [_messageButton setImage:[UIImage imageNamed:@""] forState:0];
    [_messageButton adjustsImageWhenHighlighted];
    _messageButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    //    _messageButton.titleEdgeInsets = UIEdgeInsetsMake(0, 2* 5, 0, 0);
    //    _messageButton.imageEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
//    [_messageButton addTarget:self action:@selector(messButtonBarItemClick) forControlEvents:UIControlEventTouchUpInside];
    _messageButton.frame = CGRectMake(kScreenWidth - 44, 20, 44, 44);
    [self.view addSubview:_messageButton];
}


#pragma mark - Getter

#pragma mark - Setter

#pragma mark - -------<UITableViewDataSource>-------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return RecommendTypeCount;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HRZRecommendSupplierCell * cell = [tableView dequeueReusableCellWithIdentifier:HRZRecommendSupplierCellID];
    
    [cell updateConstraints];
    
    
    return cell;
}


#pragma mark - TableViewHeader - Set
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) return 180;
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) return self.bannerAd;
    return nil;
}

#pragma mark - -------<HRZRecommendSupplierCellDelegate>-------
- (void)recommendSupplierCell:(UITableViewCell *)cell didSelectedItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HRZStoreViewController *storeVC = [[HRZStoreViewController alloc] init];

    
    [self.navigationController pushViewController:storeVC animated:YES];
    
}

#pragma mark - -------<SDCycleScrollViewDelegate>-------
/** 点击AD图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    UIViewController *newVC = [[UIViewController alloc] init];
    newVC.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:newVC animated:YES];
}

@end
